package com.jfeat.am.module.wegou.api.patch;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.jfeat.am.common.constant.tips.ErrorTip;
import com.jfeat.am.common.constant.tips.SuccessTip;
import com.jfeat.am.common.constant.tips.Tip;
import com.jfeat.am.common.controller.BaseController;
import com.jfeat.am.common.crud.CRUD;
import com.jfeat.am.common.exception.BizExceptionEnum;
import com.jfeat.am.common.persistence.model.User;
import com.jfeat.am.modular.system.service.UserService;
import com.jfeat.am.module.wegou.constant.IdentityEnum;
import com.jfeat.am.module.wegou.exception.WegouBizException;
import com.jfeat.am.module.wegou.services.crud.service.UserIdentityService;
import com.jfeat.am.module.wegou.services.domain.model.RegisterModel;
import com.jfeat.am.module.wegou.services.domain.model.UserModel;
import com.jfeat.am.module.wegou.services.domain.service.QueryUserService;
import com.jfeat.am.module.wegou.services.persistence.model.UserIdentity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2018/2/3.
 */
@Api(value = "管理员用的Oauth")
@RequestMapping("/api/wegou/oauth")
@RestController
public class WegouOauthEndpoint extends BaseController {

    @Resource
    UserService userService;
    @Resource
    UserIdentityService userIdentityService;
    @Resource
    QueryUserService queryUserService;

    @PostMapping("/account")
    @ApiOperation(value = "管理员后台直接添加平台用户账号")
    @com.jfeat.am.module.wegou.annotation.UserIdentity   //默认检测Admin
    public Tip addPlatformAccount(@RequestBody RegisterModel model) {
        //validate
        if (userService.getByAccount(model.getAccount()) != null) {
            return ErrorTip.create(BizExceptionEnum.USER_ALREADY_REG.getCode(), BizExceptionEnum.USER_ALREADY_REG.getMessage());
        }

        //add user
        User user = new User();
        user.setAccount(model.getAccount());
        user.setPassword(model.getPassword());
        boolean saveUserResult = userService.saveUser(user, null);

        //add  userIdentity
        if (saveUserResult) {
            UserIdentity userIdentity = new UserIdentity();
            userIdentity.setIdentity(IdentityEnum.PLATFORM.toString());
            userIdentity.setUserId(user.getId());
            userIdentity.setNote("平台用户");
            return SuccessTip.create(userIdentityService.createMaster(userIdentity));
        }
        return ErrorTip.create(WegouBizException.ACCOUNT_CREATE_FAILURE.getCode(), WegouBizException.ACCOUNT_CREATE_FAILURE.getMessage());
    }

    @GetMapping("/account")
    @ApiOperation(value = "管理员查看账号列表")
    @com.jfeat.am.module.wegou.annotation.UserIdentity
    public Tip queryAccounts(Page<UserModel> page,
                             @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                             @RequestParam(name = "identity", required = false) String identity,
                             @RequestParam(name = "account", required = false) String account) {
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        User user = new User();
        user.setAccount(account);
        List<UserModel> users = queryUserService.findUserPage(page, user, identity);
        page.setRecords(users);
        return SuccessTip.create(page);
    }

    @GetMapping("/account/{id}")
    @ApiOperation(value = "管理员查看账号")
    @com.jfeat.am.module.wegou.annotation.UserIdentity
    public Tip getAccount(@PathVariable Long id) {
        User user = userService.getById(id);
        UserIdentity userIdentity = userIdentityService.findFirstByUserId(user.getId());
        UserModel userModel = new UserModel();
        if (userIdentity != null) {
            userModel.setIdentity(userIdentity.getIdentity());
        }
        JSONObject userJsonObject = CRUD.toJSONObject(user);
        JSONObject userModelJsonObject = CRUD.toJSONObject(userModel);
        CRUD.copyFrom(userModelJsonObject, userJsonObject, true);

        return SuccessTip.create(userModelJsonObject.toJavaObject(UserModel.class));
    }


}
